<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工单标题" prop="workName">
        <el-input
          v-model="queryParams.workName"
          placeholder="请输入工单标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单号" prop="workId">
        <el-input
          v-model="queryParams.workId"
          placeholder="请输入工单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="company">
        <el-input
          v-model="queryParams.company"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程名称" prop="templateId">
        <el-select v-model="queryParams.templateId" placeholder="请选择">
          <el-option
            v-for="item in flowOptions"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="运行状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择">
          <el-option
            v-for="item in stateOptions"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发起人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入发起人姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组织名称" prop="orgName">
        <el-input
          v-model="queryParams.orgName"
          placeholder="请输入申请人组织名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理人" prop="ownerName">
        <el-input
          v-model="queryParams.ownerName"
          placeholder="请输入当前处理人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeStartTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="完成时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeFinishTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['workOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="flowMainInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工单号" align="center" prop="workId" />
      <el-table-column label="工单标题" align="center" prop="workName" />
      <el-table-column label="申请人姓名" align="center" prop="userName" />
      <el-table-column label="发起时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="组织名称" align="center" prop="orgName" />
      <el-table-column label="运行状态" align="center" prop="state">
        <template #default="scope">
          <dict-tag :options="flow_status" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="finishTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="公司名称" align="center" prop="company" />
      <el-table-column label="当前节点" align="center" prop="nodeName" />
      <el-table-column label="当前处理人" align="center" prop="ownerName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup>
import {getFlowInfo, listFlowMainInfo, getFlowState} from "@/api/biz/workOrder";

const { proxy } = getCurrentInstance();
const { flow_status } = proxy.useDict("flow_status");
const flowMainInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeStartTime = ref([]);
const daterangeFinishTime = ref([]);
const flowOptions = ref([]);
const stateOptions = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    workId: null,
    workName: null,
    userId: null,
    userName: null,
    orgName: null,
    ownerName: null,
    company: null,
    templateId: null,
    state: null,
  }
});

const { queryParams, form } = toRefs(data);

/** 查询流程工单记录列表 */
async function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (daterangeStartTime) {
    // daterangeStartTime时间范围控件获取的值：数组 [开始时间，结束时间]
    queryParams.value.params["beginStartTime"] = daterangeStartTime.value[0];
    queryParams.value.params["endStartTime"] = daterangeStartTime.value[1];
  }
  if (daterangeFinishTime) {
    // daterangeFinishTime时间范围控件获取的值：数组 [开始时间，结束时间]
    queryParams.value.params["beginFinishTime"] = daterangeFinishTime.value[0];
    queryParams.value.params["endFinishTime"] = daterangeFinishTime.value[1];
  }
  const response  = await listFlowMainInfo(queryParams.value);
  flowMainInfoList.value = response.rows;
  total.value = response.total;
  loading.value = false;
}

async function getFlowInfos() {
  const response = await getFlowInfo();
  flowOptions.value = response.data;
}

async function getFlowStateInfo() {
  const response = await getFlowState();
  stateOptions.value = response.data;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}



/** 导出按钮操作 */
function handleExport() {
  proxy.download('/biz/workOrder/export', {
    ...queryParams.value
  }, `综合查询_${new Date().getTime()}.xlsx`)
}
getFlowInfos();
getFlowStateInfo();
getList();
</script>
